﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SettingCtrl : Step {

    private readonly string prefabPath = "Prefabs/settingPanel";
    private GameObject settingPanel = null;
    private UIButton exitBtn = null;
    private UIButton praiseBtn = null;
    private UIButton aboutMeBtn = null;
    private UILabel titleLab = null;
    private Dictionary<string, SettingParam> itemDict = new Dictionary<string, SettingParam>();
    private List<SettingItem> itemList = new List<SettingItem>();

    public override void Init()
    {
        if (settingPanel == null)
        {
            var go = Resources.Load(prefabPath) as GameObject;
            settingPanel = GameObject.Instantiate<GameObject>(go);
            settingPanel.name = "SettingCtrl";

            var trans = settingPanel.transform;
            trans.SetParent(NGUITools.TryGetUIRoot().transform);
            trans.localScale = Vector3.one;
            trans.localPosition = Vector3.one;
            trans.localEulerAngles = Vector3.zero;

            itemDict.Add(TestKey.TomatoTask, GetItemParam(TestKey.TomatoTask));
            itemDict.Add(TestKey.NormalRestTask, GetItemParam(TestKey.NormalRestTask));
            itemDict.Add(TestKey.NormalToSpecailRestTaskInterval, GetItemParam(TestKey.NormalToSpecailRestTaskInterval));
            itemDict.Add(TestKey.SpecailRestTask, GetItemParam(TestKey.SpecailRestTask));

            InitPanel();
        }

        settingPanel.SetActive(true);
        int index = 0;
        foreach (var itemP in itemDict)
        {
            var param = GetItemParam(itemP.Key);
            if (index < itemList.Count)
            {
                itemList[index].UpdateItem(param);
                itemList[index].SetActive(true);
                ++index;
            }
        }
        if (index < itemList.Count)
        {
            for (; index < itemList.Count; ++index)
            {
                itemList[index].SetActive(false);
            }
        }
      
    }

    private void InitPanel()
    {
        var trans = settingPanel.transform;
        exitBtn = trans.Find("exitBtn").GetComponent<UIButton>();
        praiseBtn = trans.Find("praiseBtn").GetComponent<UIButton>();
        aboutMeBtn = trans.Find("aboutMeBtn").GetComponent<UIButton>();
        titleLab = trans.Find("titleWidget/Label").GetComponent<UILabel>();

        exitBtn.onClick.Add(new EventDelegate(() =>
        {
            this.Exit();
        }));

        praiseBtn.onClick.Add(new EventDelegate(() =>
        {
            BeforeExit();
            PraiseCtrl.Instance.Enter(this);
        }));

        aboutMeBtn.onClick.Add(new EventDelegate(() =>
        {
            BeforeExit();
            AboutMeCtrl.Instance.Enter(this);
        }));

        titleLab.text = "设置";

        var gridTrans = trans.Find("grid");
        for (int i = 0; i < gridTrans.childCount; ++i)
        {
            var item = new SettingItem(gridTrans.GetChild(i));
            itemList.Add(item);
        }
    }

    private SettingParam GetItemParam(string key)
    {
        SettingParam param = null;
        if (itemDict.ContainsKey(key))
        {
            param = itemDict[key];
        }
        else
        {
            // 生成 string desStr, int initVal, string unit, ChangeValueCallBack callBack, InvokeFunc desFunc
            string desStr = "";
            string unit = "";
            ChangeValueCallBack callBack = null;
            InvokeFunc desFunc = null;
            if (key == TestKey.TomatoTask)
            {
                desStr = "番茄钟时间:";
                unit = "分";
                callBack = (int newVal) =>
                {
                    PlayerPrefs.SetInt(TestKey.TomatoTask, newVal * 60);
                };
                desFunc = () =>
                {
                    NoticeCtrl.Instance.Open("番茄钟时间就是工作时间,工作时间从头到尾不能够被打断，打断就得重来", (bool isYes) => { });
                };
            }

            if (key == TestKey.NormalRestTask)
            {
                desStr = "短休息时间:";
                unit = "分";
                desFunc = () =>
                {
                    NoticeCtrl.Instance.Open("大脑休息时间，休息时间尽量放松自己，不要想着工作的事", (bool isYes) => { });
                };
                callBack = (int newVal) =>
                {
                    PlayerPrefs.SetInt(TestKey.NormalRestTask, newVal * 60);
                };
            }


            if (key == TestKey.NormalToSpecailRestTaskInterval)
            {
                desStr = "长短修间隔:";
                unit = "次";
                desFunc = () =>
                {
                    NoticeCtrl.Instance.Open("每N次小休息后插入一个大休息时间，建议半小时", (bool isYes) => { });
                };
                callBack = (int newVal) =>
                {
                    PlayerPrefs.SetInt(TestKey.NormalToSpecailRestTaskInterval, newVal);
                };
            }

            if (key == TestKey.SpecailRestTask)
            {
                desStr = "长休息时间:";
                unit = "分";
                desFunc = () =>
                {
                    NoticeCtrl.Instance.Open("大脑休息时间，休息时间尽量放松自己，不要想着工作的事", (bool isYes) => { });
                };
                callBack = (int newVal) =>
                {
                    PlayerPrefs.SetInt(TestKey.SpecailRestTask, newVal * 60);
                };
            }

            param = new SettingParam(desStr, 0, unit, callBack, desFunc);
        }


        if (key == TestKey.TomatoTask)
        {
            param.initVal = PlayerPrefs.GetInt(TestKey.TomatoTask) / 60;
        }
        else if (key == TestKey.NormalRestTask)
        {
            param.initVal = PlayerPrefs.GetInt(TestKey.NormalRestTask) / 60;
        }
        else if (key == TestKey.NormalToSpecailRestTaskInterval)
        {
            param.initVal = PlayerPrefs.GetInt(TestKey.NormalToSpecailRestTaskInterval);
        }
        else if (key == TestKey.SpecailRestTask)
        {
            param.initVal = PlayerPrefs.GetInt(TestKey.SpecailRestTask) / 60;
        }

        return param;
    }

    public override void BeforeExit()
    {
        settingPanel.SetActive(false);
    }
}

delegate void ChangeValueCallBack(int changeVal);

class SettingParam 
{
    public readonly string desStr;
    public int initVal;         // 比较特殊 能够被改变
    public readonly string unit;
    public readonly ChangeValueCallBack callBack;
    public readonly InvokeFunc desFunc;
    public SettingParam(string desStr, int initVal, string unit, ChangeValueCallBack callBack, InvokeFunc desFunc)
    {
        this.desStr = desStr;
        this.initVal = initVal;
        this.unit = unit;
        this.callBack = callBack;
        this.desFunc = desFunc;
    }
}
class SettingItem
{
    private Transform root = null;
    private UILabel desLab = null;
    private UILabel numLab = null;
    private UILabel unitLab = null;
    private UIButton addBtn = null;
    private UIButton subBtn = null;
    private UIButton desBtn = null;
    private SettingParam param = null;

    public SettingItem(Transform root)
    {
        this.root = root;
        desLab = root.Find("desLab").GetComponent<UILabel>();
        numLab = root.Find("Sprite/numLab").GetComponent<UILabel>();
        unitLab = root.Find("Sprite/unitLab").GetComponent<UILabel>();
        addBtn = root.Find("addBtn").GetComponent<UIButton>();
        subBtn = root.Find("subBtn").GetComponent<UIButton>();
        desBtn = root.Find("desBtn").GetComponent<UIButton>();

        addBtn.onClick.Add(new EventDelegate(OnAddClick));
        subBtn.onClick.Add(new EventDelegate(OnSubClick));
        desBtn.onClick.Add(new EventDelegate(OnDesClick));
    }

    public void UpdateItem(SettingParam param) 
    {
        this.param = param;
        numLab.text = param.initVal.ToString();
    }

    private void OnAddClick()
    {
        if (this.param != null)
        {
            var newVal = this.param.initVal + 1;
            if (newVal > 60) newVal = 60;
           
            this.param.initVal = newVal;
            numLab.text = newVal.ToString();
            this.param.callBack(newVal);
        }
    }

    private void OnSubClick()
    {
        if (this.param != null)
        {
            var newVal = this.param.initVal - 1;
            if (newVal < 0) newVal = 0;

            this.param.initVal = newVal;
            numLab.text = newVal.ToString();
            this.param.callBack(newVal);
        }
    }

    private void OnDesClick()
    {
        this.param.desFunc();
    }

    public void SetActive(bool isActive)
    {
        this.root.gameObject.SetActive(isActive);
        if (!isActive) this.param = null;
    }
}